<?php

namespace app\bis\controller;

use think\Controller;

class Register extends Controller
{
    public function index()
    {
        $citys = model('city')->getNormalCitysByParentId();
        $category = model('category')->getNormalFirstCategory();
        $data = [
            'citys' => $citys,
            'category' => $category
        ];
        return $this->fetch('/register/index', $data);
    }

    public function add()
    {
        if (!request()->isPost()) {
            $this->error('请求错误');
        }
        //获取表单的数据
        $data = input('post.', '', 'htmlentities');
//        echo "<pre>";
//        print_r($data);die;
        //校验数据
        $validate = validate('user');
        if (!$validate->scene('add')->check($data)) {
            $this->error($validate->getError());
        }
        $validate = validate('Bis');
        if (!$validate->scene('add')->check($data)) {
            $this->error($validate->getError());
        }
        //根据用户输入的地址获取到经纬度
        $address = \Map::getLngLat($data['address']);
        if (empty($address) || $address['status'] != 0 || $address['result']['precise'] != 1) {
            $this->error('匹配地址不精确');
        }
        //验证通过商户基本信息数据入库
        $bisData = [
            'name' => $data['name'],
            'city_id' => $data['city_id'],
            'email' => $data['email'],
            'logo' => $data['logo'],
            'licence_logo' => $data['licence_logo'],
            'description' => empty($data['description']) ? '' : $data['description'],
            'city_path' => empty($data['se_city_id']) ? $data['city_id'] : $data['city_id'] . ',' . $data['se_city_id'],
            'bank_info' => $data['bank_info'],
            'bank_name' => $data['bank_name'],
            'bank_user' => $data['bank_user'],
            'faren' => $data['faren'],
            'faren_tel' => $data['faren_tel'],
        ];
        $bisId = model('bis')->add($bisData);
        $data['cat'] = '';
        if (!empty($data['se_category_id'])) {
            $data['cat'] = implode(',', $data['se_category_id']);
        }
        //总店信息入库
        $locationData = [
            'bis_id' => $bisId,
            'name' => $data['name'],
            'logo' => $data['logo'],
            'api_address' => $data['address'],
            'tel' => $data['tel'],
            'contact' => $data['contact'],
            'xpoint' => empty($address['result']['location']['lng']) ? '' : $address['result']['location']['lng'],
            'ypoint' => empty($address['result']['location']['lat']) ? '' : $address['result']['location']['lat'],
            'category_id' => $data['category_id'],
            'category_path' => $data['category_id'] . ',' . $data['cat'],
            'city_id' => $data['city_id'],
            'city_path' => empty($data['se_city_id']) ? $data['city_id'] : $data['city_id'] . ',' . $data['se_city_id'],
            'open_time' => $data['open_time'],
            'bank_info' => $data['bank_info'],
            'content' => empty($data['content']) ? '' : $data['content'],
            'is_main' => 1
        ];
        model('BisLocation')->add($locationData);
        //账户信息入库
        //盐值
        $salt = mt_rand(100000, 999999);
        $userData = [
            'username' => $data['username'],
            'password' => MD5($data['password'] . $salt),
            'code' => $salt,
            'bis_id' => $bisId,
            'is_main' => 1,
        ];
        $addountId = model('BisAccount')->add($userData);
        if (!$addountId) {
            $this->error('申请失败');
        }
        //发送邮件
        $url = request()->domain() . url('bis/register/waiting', ['id' => $bisId]);
        $title = 'o2o申请入驻通知';
        $content = "您提交的入驻申请需要等待平台方审核，您可以通过点击<a href='" . $url . "' target='_blank'>查看链接</a> 查看审核状态";
        \phpmailer\Email::send($data['email'], $title, $content);

        $this->success('申请成功', url('register/waiting', ['id' => $bisId]));
    }

    public function waiting($id)
    {
        if (empty($id)) {
            $this->error('error');
        }
        $detail = model('Bis')->get($id);
        return $this->fetch('', ['detail' => $detail]);
    }
}